package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.Config;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.service.ConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *   @description : Config 管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-01-07
 */
@RestController
@Api(value="/config", description="Config 管理")
@RequestMapping("api/config")
public class ConfigController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    public ConfigService configService;

    /**
     * @description : 获取Config分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-07
     */
    @RequiresPermissions("dev:config:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page( Integer pageSize, Integer pageNum) {
        Wrapper<Config> ew = new EntityWrapper<Config>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<Config> page = new Page<Config>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = configService.selectPage(page, ew);

        return ResultMessage.success("获取Config的分页列表",page);
    }

    /**
     * @description : 获取所有Config记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-07
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有Config信息", notes="获取所有Config信息")
    public ResultMessage list() {
        List<Config> list= configService.selectList(new EntityWrapper<Config>().where("deleted=0"));
        return ResultMessage.success("获取所有Config信息",list);
    }

    /**
     * @description : 通过id获取Config
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-07
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取Config信息", notes="获取Config信息")
    @ApiImplicitParam(name="id",value="ConfigID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取Config信息成功!",configService.selectById(id));
    }

    /**
     * @description : 添加Config
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-07
     */
    @RequiresPermissions("dev:config:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(Config config){
        configService.insert(config);
        return ResultMessage.success("添加Config成功!");
    }

    /**
     * @description : 通过id逻辑删除Config
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-07
     */
    @RequiresPermissions("dev:config:remove")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    public ResultMessage removeFlag(Long id){
        if(configService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑Config删除成功！");
        } else {
            return ResultMessage.fail("逻辑Config删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除Config
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-07
     */
    @RequiresPermissions("dev:config:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public ResultMessage remove(long id){
        if(configService.deleteById(id)) {
            return ResultMessage.success("物理删除Config成功！");
        } else {
            return ResultMessage.fail("物理删除Config失败！");
        }
    }

    /**
     * @description : 更新Config信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-07
     */
    @RequiresPermissions("dev:config:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultMessage update(Config config) {
        if(configService.updateById(config)) {
            return ResultMessage.success("Config信息更新成功!");
        }else{
            return ResultMessage.fail("Config信息更新失败!");
        }
    }

    /**
     * @description : 更新SysConfig信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-12-05
     */

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResultMessage update(HttpServletRequest request) {
        Map<String,String> reqMap = new HashMap<>();
        Map<String,String[]> map = request.getParameterMap();
        Set<String> keys = map.keySet();
        for(String key : keys) {
            String[] value = map.get(key);
            reqMap.put(key, value[0]);
        }

        if(configService.save(reqMap)) {
            return ResultMessage.success("Config信息更新成功!");
        }else{
            return ResultMessage.fail("Config信息更新失败!");
        }
    }

    /**
    * @description : 清除Config记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-07
    */
    @RequiresPermissions("dev:config:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    public ResultMessage clear(){
        int num = configService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有Config记录被清除！");
        }
    }

}
